package com.huifer.wgx.controller;

import com.huifer.wgx.model.FieldPointEntity;
import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.service.FieldPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : FieldPointController </p>
 * <p>Description : 外业采集点</p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@RequestMapping("field_point")
@Api(tags = "FieldPointController", description = "外业采集点管理")
public class FieldPointController {

    @Autowired
    private FieldPointService fieldPointService;

    private CommonResult commonResult = new CommonResult();


    @ApiOperation("创建一个外业点")
    @PostMapping("/save_one")
    public ResponseEntity saveOneFiPoint(
            @RequestParam("field_point_name") String fieldPointName,
            @RequestParam("field_point_manhole_cover_type") String fieldPointManholeCoverType,
            @RequestParam("field_point_ismesh") String fieldPointIsmesh,
            @RequestParam("field_point_deep") String fieldPointDeep,
            @RequestParam("field_point_size") String fieldPointSize,
            @RequestParam("personnel_id") Long personnelId,
            @RequestParam("work_id") Long workId) {
        try {
            return ResponseEntity.ok(commonResult
                    .success(fieldPointService.saveOneFieldPoint(fieldPointName,
                            fieldPointManholeCoverType,
                            fieldPointIsmesh,
                            new BigDecimal(fieldPointDeep),
                            fieldPointSize,
                            personnelId,
                            workId)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("将外业采集点保存到数据库")
    @PostMapping("/save")
    public ResponseEntity saveAllFiPoint(
            @RequestParam("personnel_id") Long personnelId,
            @RequestParam("work_id") Long workid,
            @RequestParam("filename") String filename
    ) {
        try {
            List<FieldPointEntity> fieldPointServiceList = fieldPointService
                    .createList(personnelId, workid, filename);
            return ResponseEntity.ok(commonResult
                    .success(fieldPointService.saveFieldPoint(fieldPointServiceList)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("查询所有外业采集点")
    @GetMapping("/find_all")
    public ResponseEntity findAllFieldPoint(
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(fieldPointService.findAll(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("根据工程id查询外业采集点")
    @GetMapping("/find_by_work_id")
    public ResponseEntity findRtkByWorkId(
            @RequestParam("workid") Long workId,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult
                            .success(fieldPointService.findByWorkId(pageNum, pageSize, workId)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("根据采集人id查询外业采集点")
    @GetMapping("/find_by_personnel_id")
    public ResponseEntity findByPersonnelId(
            @RequestParam("personnel_id") Long personnelId,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(
                            fieldPointService.findByPersonnelId(personnelId, pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

}
